package org.unisinos.gc.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.unisinos.gc.dao.AutorDAO;
import org.unisinos.gc.dao.UtilDAO;
import org.unisinos.gc.form.AutorForm;
import org.unisinos.gc.validation.AutorValidation;
import org.unisinos.gc.vo.Autor;

public class AutorAction extends Action {

	AutorForm autorForm;
	String pageReturn = "list";
	ActionMessages actionMessages;
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		autorForm = (AutorForm) form;
		actionMessages = new ActionMessages();
		if (autorForm.getAcao() != null) {
			if (autorForm.getAcao().equals(AutorForm.SAVE)) {
				save(request);
				request.setAttribute("mode", "edit");
			} else if (autorForm.getAcao().equals(AutorForm.LIST)) {
				list(request);
			} else if (autorForm.getAcao().equals(AutorForm.EDIT)) {
				edit(request);
			} else if (autorForm.getAcao().equals(AutorForm.DELETE)) {
				delete();
				request.setAttribute("mode", "edit");
			} else if (autorForm.getAcao().equals(AutorForm.SAVE_NEW)) {
				saveNew(request);
				request.setAttribute("mode", "new");
			} else {
				list(request);
			}
		} else {
			list(request);
		}
		
		if (!actionMessages.isEmpty()) 
			super.saveMessages(request, actionMessages);
		return mapping.findForward(pageReturn);
	}
	
	private void list(HttpServletRequest request) {
		request.setAttribute("autorList", AutorDAO.getAutorList());
		autorForm.setAutores(AutorDAO.getAutorList());
		pageReturn = "list";
	}
	
	private void edit(HttpServletRequest request) {
		String codigo = (String)request.getParameter("codigo");
		if (StringUtils.isNotEmpty(codigo)) {
			Autor autor = AutorDAO.getAutor(Integer.parseInt(codigo));		
			autorForm.setAutor(autor);
			request.setAttribute("mode", "edit");
		}else {
			request.setAttribute("mode", "new");
		}
		pageReturn = "success";
	}
	
	private void save(HttpServletRequest request) {
		String erro = AutorValidation.validaUpdate(autorForm.getAutor());
		if (erro == null) {		
			AutorDAO.updateAutor(autorForm.getAutor());
			actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("autor.saved.success"));
		} else {
			ActionErrors errors = new ActionErrors();
			errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("autor.error", erro));
			super.saveErrors(request, errors);
		}

		pageReturn = "success";
	}
	
	private void saveNew(HttpServletRequest request) {
		String erro = AutorValidation.validaSaveNew(autorForm.getAutor());
		if (erro == null) {
			Autor autor = autorForm.getAutor();
			autor.setCodigo(UtilDAO.getNextCodigo("autor", "codigo"));
			AutorDAO.saveNewAutor(autor);
			actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("autor.saved.success"));
			pageReturn = "success";
		} else {
			ActionErrors errors = new ActionErrors();
			errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("autor.error", erro));
			super.saveErrors(request, errors);
		}
	}
	
	private void delete() {
		int codigo = autorForm.getAutor().getCodigo();
		AutorDAO.deleteAutor(codigo);
		pageReturn = "doList";
	}

	public AutorForm getautorForm() {
		return autorForm;
	}

	public void setautorForm(AutorForm autorForm) {
		this.autorForm = autorForm;
	}
	
	
}
